// pages/my/my.js
var app = getApp();
var util = require('../../utils/util.js');
Page({
  data: {
    userInfo: {},
    list: [],
    goodList: [],
    showNoneRefresh: false,
    imageUrl: app.imageUrl,
    pageIndex: 1,
    total: 0,
    imageUrl: app.imageUrl,
    showNoneRefresh: false,
    gpId: "",
    styles: "",
    // 规格弹出层
    showLoadIngGg: true,
    isTrueGroup: false, // 是否开团
    goods: '', // 产品
    ggList: '', // 产品规格集合
    ggListArr: '', // 筛选规格
    ggValue0: '',
    ggValue1: '',
    ggValue2: '',
    isChoice: false,
    vaArr: [],
    goodsGg: {}, // 选中规格
    chPrice: '',
    onePrice: '', // 单价
    chStock: '',
    ddPrice: 0,
    ptPrice: 0,
    goodNum: '1',
    filter: ""
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.init()
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  onPullDownRefresh: function () {
    if (app.goodTypes.length > 0) {
      this.setData({ pageIndex: 1, total: 0, showNoneRefresh: false })
      var params = {
        shopId: app.shopId, pageIndex: this.data.pageIndex, pageSize: 5,
        gpId: this.data.gpId, showNoneRefresh: false, goodsGroupId: this.data.gpId
      }
      this.getListByGroupId(params, true)
    }
  },
  init: function () {
    if (app.goodTypes.length > 0) {
      var gpId = app.goodTypes[0].id
      this.setData({
        list: app.goodTypes, styles: "color:#0A77F7",
        gpId: gpId
      })
      var params = { shopId: app.shopId, pageIndex: this.data.pageIndex, pageSize: 5, goodsGroupId: gpId }
      this.getListByGroupId(params, true)
    }
  },
  getListByGroupId: function (params, flag) {
    wx.showLoading({
      title: '加载中',
    })
    var that = this
    if (this.data.filter != "" && this.data.filter != null) {
      params.goodsName = this.data.filter
    }
    var encodingStr = app.ecbEncrypto(params)
    wx.request({
      url: app.url + '/cx/retail/getGoodsList.htm',
      data: encodingStr,
      method: 'POST',
      success: function (res) {
        var decodingStr = app.ecbDecrypto(res.data)
        var rest = JSON.parse(decodingStr)
        console.log(rest)
        if (rest.isSuccess == 0) {
          if (rest.list) {
            var tempList = flag ? [] : that.data.goodList
            var list = rest.list
            for (var i = 0; i < list.length; i++) {
                if (list[i].goodsName.length > 20) {
                  list[i].goodsName = list[i].goodsName.substr(0, 20) + "..."
                }
                tempList.push(list[i])
            }
            that.setData({ pageIndex: that.data.pageIndex + 1, goodList: tempList, total: rest.total})
          } else {
            that.setData({ goodList: [], total: 0 })
          }
        }
      },
      complete: function () {
        wx.stopPullDownRefresh()
        wx.hideLoading()
      }
    })
  },
  changeType: function (event) {
    var gpid = event.currentTarget.dataset.gpid
    this.setData({ gpId: gpid, pageIndex: 1, total: 0, showNoneRefresh: false})
    var params = { goodsGroupId: gpid, shopId: app.shopId, pageIndex: this.data.pageIndex, pageSize: 5 }
    this.getListByGroupId(params, true)
  },
  onReachBottom: function () {
    if (this.data.goodList.length < this.data.total) {
      var params = { goodsGroupId: this.data.gpId, shopId: app.shopId, pageIndex: this.data.pageIndex, pageSize: 5}
      this.getListByGroupId(params)
    } else {
      this.setData({ showNoneRefresh: true })
    }
  },
  bindSearch: function (e) {
    wx.navigateTo({
      url: '../search/search'
    })
  },
  toDetail: function (event) {
    var gid = event.currentTarget.dataset.gid
    console.log(gid)
    wx.navigateTo({
      url: '../goods/goodDetail?id=' + gid
    })
  }
  
})